﻿#include "FormatController.h"
#include <QTextCharFormat>
#include <QDebug>
#include <QObject>
#include <formatpanel.h>

FormatController::FormatController(QObject* parent)
	: QObject(parent)
{
}


void FormatController::initConnections()
{
	RibbonToolbar* ribbon = dynamic_cast<RibbonToolbar*>(m_ribbon);
	connect(m_menubar, &MenuBar::fontStyleChange, this, &FormatController::onMenubarStyleSelected);
	connect(ribbon, &RibbonToolbar::styleSelected, this, &FormatController::onRibbonStyleSelected);
}

void FormatController::setRibbonToolbar(IToolbar* ribbon) { m_ribbon = ribbon; }
void FormatController::setFormatPanel(IFormatPanel* panel) { m_panel = panel; }
void FormatController::setActiveEditor(EditorWidget* editor) 
{ 
	disconnectEditorSignals(m_editor);
	m_editor = editor;
	if (m_editor) 
		connectEditorSignals(m_editor);
}
void FormatController::setMenuBar(MenuBar* menubar) { m_menubar = menubar; }

void FormatController::onSidebarStyleSelected(const QString& styleName)
{
	FormatPanel* panel = dynamic_cast<FormatPanel*>(m_panel);
	if (!panel || !m_editor) return;

	FormatStyle style = panel->getStyle(styleName); 

	m_editor->applyFontFamily(style.fontFamily);
	m_editor->applyFontSize(style.fontSize);
	m_editor->applyBold(style.bold);
	m_editor->applyItalic(style.italic);
	m_editor->applyUnderline(style.underline);
}

void FormatController::onEditorFormat
(
	bool isBold, bool isItalic, bool isUnderline, bool isStrikethrough,
	const QString& fontFamily, int fontSize, const QColor& fontcolor,
	const QColor& highligcolor,Qt::Alignment alignment, const QString& styleMarker
)
{
	RibbonToolbar* ribbon = dynamic_cast<RibbonToolbar*>(m_ribbon);
	FormatPanel* panel = dynamic_cast<FormatPanel*>(m_panel);
	if (!ribbon || !panel) return;

	ribbon->setBoldState(isBold);
	ribbon->setItalicState(isItalic);
	ribbon->setUnderlineState(isUnderline);
	ribbon->setStrikethroughState(isStrikethrough);
	ribbon->setFontFamily(fontFamily);
	ribbon->setFontSize(fontSize);
	ribbon->setFontColor(fontcolor);
	ribbon->setHighlightColor(highligcolor);
	ribbon->setAlignmentState(alignment, true);

	if (!styleMarker.isEmpty() && panel->getStyle(styleMarker).name == styleMarker) 
		panel->setCurrentStyle(styleMarker);
	else 
		panel->setCurrentStyle(""); 
}

void FormatController::onMenubarStyleSelected(const QString& styleName)
{
	if (!m_editor) return;
	FormatPanel* panel = dynamic_cast<FormatPanel*>(m_panel);
	FormatStyle style = panel->getStyle(styleName);
	qDebug() << styleName;
	m_editor->applyFontFamily(style.fontFamily);
	m_editor->applyFontSize(style.fontSize);
	m_editor->applyBold(style.bold);
	m_editor->applyItalic(style.italic);
	m_editor->applyUnderline(style.underline);
}
void FormatController::onRibbonStyleSelected(const QString& styleName)
{
	if (!m_editor) return;
	FormatPanel* panel = dynamic_cast<FormatPanel*>(m_panel);
	FormatStyle style = panel->getStyle(styleName);
	qDebug() << styleName;
	m_editor->applyFontFamily(style.fontFamily);
	m_editor->applyFontSize(style.fontSize);
	m_editor->applyBold(style.bold);
	m_editor->applyItalic(style.italic);
	m_editor->applyUnderline(style.underline);
}

void FormatController::disconnectEditorSignals(EditorWidget* editor) 
{
	if (!editor) return;

	RibbonToolbar* ribbon = dynamic_cast<RibbonToolbar*>(m_ribbon);
	if (ribbon)
	{
		disconnect(ribbon, &RibbonToolbar::boldToggled, editor, &EditorWidget::applyBold);
		disconnect(ribbon, &RibbonToolbar::italicToggled, editor, &EditorWidget::applyItalic);
		disconnect(ribbon, &RibbonToolbar::underlineToggled, editor, &EditorWidget::applyUnderline);
		disconnect(ribbon, &RibbonToolbar::strikethroughToggled, editor, &EditorWidget::applyStrikethrough);
		disconnect(ribbon, &RibbonToolbar::fontFamilySelected, editor, &EditorWidget::applyFontFamily);
		disconnect(ribbon, &RibbonToolbar::fontSizeSelected, editor, &EditorWidget::applyFontSize);
		disconnect(ribbon, &RibbonToolbar::fontColorSelected, editor, &EditorWidget::applyFontColor);
		disconnect(ribbon, &RibbonToolbar::highlightColorSelected, editor, &EditorWidget::applyHighlightColor);
		disconnect(ribbon, &RibbonToolbar::alignmentChanged, editor, &EditorWidget::applyParagraphAlignment);
		disconnect(ribbon, &RibbonToolbar::imageInsertRequested, editor, &EditorWidget::insertImage);
		disconnect(ribbon, &RibbonToolbar::tableInsertRequested, editor, &EditorWidget::insertTable);
		disconnect(ribbon, &RibbonToolbar::undoRequested, editor, &EditorWidget::undo);
		disconnect(ribbon, &RibbonToolbar::shapeSelected, editor, &EditorWidget::setCurrentShape);
		disconnect(ribbon, &RibbonToolbar::redoRequested, editor, &EditorWidget::redo);
	}
	if (m_menubar) 
	{
		disconnect(m_menubar, &MenuBar::undo, editor, &EditorWidget::undo);
		disconnect(m_menubar, &MenuBar::redo, editor, &EditorWidget::redo);
		disconnect(m_menubar, &MenuBar::cut, editor, &EditorWidget::cut);
		disconnect(m_menubar, &MenuBar::paste, editor, &EditorWidget::paste);
		disconnect(m_menubar, &MenuBar::copy, editor, &EditorWidget::copy);
	}
	FormatPanel* panel = dynamic_cast<FormatPanel*>(m_panel);
	if (m_panel)
	{
		disconnect(panel, &FormatPanel::styleSelected,this, &FormatController::onSidebarStyleSelected);
		disconnect(panel, &FormatPanel::clearFormatClicked, editor, &EditorWidget::clearFormat);
		disconnect(panel, &FormatPanel::resetDefaultsClicked, editor, &EditorWidget::resetToDefaultStyle);
	}
	disconnect(editor, &EditorWidget::formatChanged, this, &FormatController::onEditorFormat);
}

void FormatController::connectEditorSignals(EditorWidget* editor)
{
	if (!editor) return;

	RibbonToolbar* ribbon = dynamic_cast<RibbonToolbar*>(m_ribbon);
	if (ribbon) 
	{
		connect(ribbon, &RibbonToolbar::boldToggled, editor, &EditorWidget::applyBold);
		connect(ribbon, &RibbonToolbar::italicToggled, editor, &EditorWidget::applyItalic);
		connect(ribbon, &RibbonToolbar::underlineToggled, editor, &EditorWidget::applyUnderline);
		connect(ribbon, &RibbonToolbar::strikethroughToggled, editor, &EditorWidget::applyStrikethrough);
		connect(ribbon, &RibbonToolbar::fontFamilySelected, editor, &EditorWidget::applyFontFamily);
		connect(ribbon, &RibbonToolbar::fontSizeSelected, editor, &EditorWidget::applyFontSize);
		connect(ribbon, &RibbonToolbar::fontColorSelected, editor, &EditorWidget::applyFontColor);
		connect(ribbon, &RibbonToolbar::highlightColorSelected, editor, &EditorWidget::applyHighlightColor);
		connect(ribbon, &RibbonToolbar::alignmentChanged, editor, &EditorWidget::applyParagraphAlignment);
		connect(ribbon, &RibbonToolbar::imageInsertRequested, editor, &EditorWidget::insertImage);
		connect(ribbon, &RibbonToolbar::tableInsertRequested, editor, &EditorWidget::insertTable);
		connect(ribbon, &RibbonToolbar::undoRequested, editor, &EditorWidget::undo);
		connect(ribbon, &RibbonToolbar::shapeSelected, editor, &EditorWidget::setCurrentShape);

		connect(ribbon, &RibbonToolbar::redoRequested, editor, &EditorWidget::redo);
	}
	if (m_menubar) 
	{
		connect(m_menubar, &MenuBar::undo, editor, &EditorWidget::undo);
		connect(m_menubar, &MenuBar::redo, editor, &EditorWidget::redo);
		connect(m_menubar, &MenuBar::cut, editor, &EditorWidget::cut);
		connect(m_menubar, &MenuBar::paste, editor, &EditorWidget::paste);
		connect(m_menubar, &MenuBar::copy, editor, &EditorWidget::copy);
	}
	FormatPanel* panel = dynamic_cast<FormatPanel*>(m_panel);
	if (panel)
	{
		connect(panel, &FormatPanel::styleSelected,this, &FormatController::onSidebarStyleSelected);
		connect(panel, &FormatPanel::clearFormatClicked, editor, &EditorWidget::clearFormat);
		connect(panel, &FormatPanel::resetDefaultsClicked, editor, &EditorWidget::resetToDefaultStyle);
	}
	connect(editor, &EditorWidget::formatChanged, this, &FormatController::onEditorFormat);
}

